package org.jeecg.modules.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hc.entity.HcCompanyMerchantSetting;
import org.jeecg.modules.hc.service.IHcCompanyMerchantSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 企业商户价格设置
 * @Author: jeecg-boot
 * @Date:   2023-07-15
 * @Version: V1.0
 */
@Api(tags="企业商户价格设置")
@RestController
@RequestMapping("/hc/hcCompanyMerchantSetting")
@Slf4j
public class HcCompanyMerchantSettingController extends JeecgController<HcCompanyMerchantSetting, IHcCompanyMerchantSettingService> {
	@Autowired
	private IHcCompanyMerchantSettingService hcCompanyMerchantSettingService;


	 /**
	  * 查询商户企业价格信息
	  *
	  * @return
	  */
	 //@AutoLog(value = "企业商户价格设置-分页列表查询")
	 @ApiOperation(value="查询商户企业价格信息", notes="查询商户企业价格信息")
	 @GetMapping(value = "/queryByMerchantId")
	 public Result<?> queryByMerchantId(@RequestParam(name="companyId") String companyId,
								@RequestParam(name="merchantId") String merchantId) {
		 return Result.OK("查询成功",hcCompanyMerchantSettingService.queryByMerchantId(companyId, merchantId));
	 }

	/**
	 * 分页列表查询
	 *
	 * @param hcCompanyMerchantSetting
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "企业商户价格设置-分页列表查询")
	@ApiOperation(value="企业商户价格设置-分页列表查询", notes="企业商户价格设置-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<HcCompanyMerchantSetting>> queryPageList(HcCompanyMerchantSetting hcCompanyMerchantSetting,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HcCompanyMerchantSetting> queryWrapper = QueryGenerator.initQueryWrapper(hcCompanyMerchantSetting, req.getParameterMap());
		Page<HcCompanyMerchantSetting> page = new Page<HcCompanyMerchantSetting>(pageNo, pageSize);
		IPage<HcCompanyMerchantSetting> pageList = hcCompanyMerchantSettingService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hcCompanyMerchantSetting
	 * @return
	 */
	@AutoLog(value = "企业商户价格设置-添加")
	@ApiOperation(value="企业商户价格设置-添加", notes="企业商户价格设置-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody HcCompanyMerchantSetting hcCompanyMerchantSetting) {
		HcCompanyMerchantSetting merchantSetting = hcCompanyMerchantSettingService.getOne(new LambdaQueryWrapper<HcCompanyMerchantSetting>()
				.eq(HcCompanyMerchantSetting::getCompanyId, hcCompanyMerchantSetting.getCompanyId())
				.eq(HcCompanyMerchantSetting::getMerchantId, hcCompanyMerchantSetting.getMerchantId()).last("limit 1"));
		if (merchantSetting == null){
			hcCompanyMerchantSettingService.save(hcCompanyMerchantSetting);
		}else {
			hcCompanyMerchantSetting.setId(merchantSetting.getId());
			hcCompanyMerchantSettingService.updateById(hcCompanyMerchantSetting);
		}
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hcCompanyMerchantSetting
	 * @return
	 */
	@AutoLog(value = "企业商户价格设置-编辑")
	@ApiOperation(value="企业商户价格设置-编辑", notes="企业商户价格设置-编辑")
	//@RequiresPermissions("org.jeecg.modules:hc_company_merchant_setting:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HcCompanyMerchantSetting hcCompanyMerchantSetting) {
		hcCompanyMerchantSettingService.updateById(hcCompanyMerchantSetting);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "企业商户价格设置-通过id删除")
	@ApiOperation(value="企业商户价格设置-通过id删除", notes="企业商户价格设置-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_merchant_setting:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		hcCompanyMerchantSettingService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "企业商户价格设置-批量删除")
	@ApiOperation(value="企业商户价格设置-批量删除", notes="企业商户价格设置-批量删除")
	//@RequiresPermissions("org.jeecg.modules:hc_company_merchant_setting:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hcCompanyMerchantSettingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "企业商户价格设置-通过id查询")
	@ApiOperation(value="企业商户价格设置-通过id查询", notes="企业商户价格设置-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<HcCompanyMerchantSetting> queryById(@RequestParam(name="id",required=true) String id) {
		HcCompanyMerchantSetting hcCompanyMerchantSetting = hcCompanyMerchantSettingService.getById(id);
		if(hcCompanyMerchantSetting==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hcCompanyMerchantSetting);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hcCompanyMerchantSetting
    */
    //@RequiresPermissions("org.jeecg.modules:hc_company_merchant_setting:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HcCompanyMerchantSetting hcCompanyMerchantSetting) {
        return super.exportXls(request, hcCompanyMerchantSetting, HcCompanyMerchantSetting.class, "企业商户价格设置");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("hc_company_merchant_setting:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HcCompanyMerchantSetting.class);
    }

}
